package edu.tum.cs.stageflow.benchmarkapplications.bigData;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import com.scireum.open.xml.NodeHandler;
import com.scireum.open.xml.StructuredNode;
import com.scireum.open.xml.XMLReader;

import edu.tum.cs.stageflow.executionframework.Flow;

public class WikiNodeHandler extends Thread {

	FileInputStream fileInput;
	private XMLReader reader;
	private Flow dataFlow;
	
	public WikiNodeHandler(Flow outputFlow) throws FileNotFoundException {
		dataFlow = outputFlow;
		fileInput = new FileInputStream("F:/Wikipedia/enwiki-latest-pages-articles.xml");
		reader = new XMLReader();	
   		reader.addHandler("page", new NodeHandler() {
			
			@Override
			public void process(StructuredNode node) {
				try {
					dataFlow.put(node);
				}
				catch (InterruptedException ex) {
					ex.printStackTrace();
				}
				
			}
		});
	}

	@Override
	public void run() {
   		try {
   			System.out.println("Starting Parser...");
			reader.parse(fileInput);
			System.out.println("Parser Done");
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
